
package com.dayaokeji.rhythmschoolstudent.client.common.exam


import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.dayaokeji.rhythmschoolstudent.R
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseFragment
import com.dayaokeji.rhythmschoolstudent.client.common.exam.adapter.SubjectResultViewPagerAdapter
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.BlankSubjectFragment
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.ChooseSubjectFragment
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.JudgmentProblemFragment
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.QuestionAndAnswerFragment
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.utils.SubjectTypeUtil
import com.dayaokeji.server_api.domain.ExamQuestion


/**
 * 题目结果
 *
 */
class SubjectResultFragment : BaseFragment() {

    companion object {

        private const val EXTRA_EXAM_QUESTION = "exam_question"

        fun newInstance(examQuestion: ExamQuestion): SubjectResultFragment {
            val bundle = Bundle()
            val subjectResultFragment = SubjectResultFragment()
            bundle.putSerializable(EXTRA_EXAM_QUESTION,examQuestion)
            subjectResultFragment.arguments = bundle
            return subjectResultFragment
        }
    }

    private lateinit var vpSubject: ViewPager
    private lateinit var tbSubjectResult: TabLayout
    private val fragments: ArrayList<Fragment> by lazy { arrayListOf<Fragment>() }

    override fun onCreate(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View {
        val view = inflater.inflate(R.layout.fragment_subject_result, container, false)
        vpSubject = view.findViewById(R.id.vp_subject)
        tbSubjectResult = view.findViewById(R.id.tb_subject_result)
        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val examQuestion = arguments?.getSerializable(EXTRA_EXAM_QUESTION) as ExamQuestion

        val orlSubjectFragment = subjectFragmentByType(examQuestion, false)
        val answerSubjectFragment = subjectFragmentByType(examQuestion, true)

        if (orlSubjectFragment != null && answerSubjectFragment != null) {
            fragments.add(orlSubjectFragment)
            fragments.add(answerSubjectFragment)
        }

        val subjectResultViewPagerAdapter = SubjectResultViewPagerAdapter(childFragmentManager, fragments)
        vpSubject.adapter = subjectResultViewPagerAdapter
        tbSubjectResult.setupWithViewPager(vpSubject)
    }

    private fun subjectFragmentByType(examQuestion: ExamQuestion,isShowResult: Boolean): Fragment? {
        val type = examQuestion.type
        when (type) {
            SubjectTypeUtil.SINGLE_CHOOSE -> return ChooseSubjectFragment.newInstance(examQuestion).setShowExamResult(true).setShowResult(isShowResult)
            SubjectTypeUtil.MULTIPLE_CHOOSE -> return ChooseSubjectFragment.newInstance(examQuestion).setShowExamResult(true).setShowResult(isShowResult)
            SubjectTypeUtil.QUESTION_AND_ANSWER -> return QuestionAndAnswerFragment.newInstance(examQuestion).setShowExamResult(true).setShowResult(isShowResult)
            SubjectTypeUtil.BLANK_SUBJECT -> return BlankSubjectFragment.newInstance(examQuestion).setShowExamResult(true).setShowResult(isShowResult)
            SubjectTypeUtil.JUDGMENT_PROBLEM -> return JudgmentProblemFragment.newInstance(examQuestion).setShowExamResult(true).setShowResult(isShowResult)
            else -> return null
        }
    }
}
